/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.khuyenmai;

import dao.khuyenmaiDAO;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.Chuongtrinhkhuyenmai;
import util.Utils;

/**
 *
 * @author lhaquang
 */
public class QuanLyCacChuongTrinhKhuyenMai extends javax.swing.JFrame {

    private DefaultTableModel tableModel;
    private List<Chuongtrinhkhuyenmai> dsCTKM;
    private List<Chuongtrinhkhuyenmai> listSelected = null;

    /**
     * Creates new form QuanLyCacChuongTrinhKhuyenMai
     */
    public QuanLyCacChuongTrinhKhuyenMai() {
        initComponents();
        this.tableModel = new DefaultTableModel();
        this.tableModel.addColumn("MA CCKM");
        this.tableModel.addColumn("NGAY BAT DAU");
        this.tableModel.addColumn("NGAY KET THUC");
        this.tableModel.addColumn("CAC MAT HANG AP DUNG");
        this.tableModel.addColumn("GHI CHU");
        this.jtDSCTKM.setModel(tableModel);
        this.dsCTKM = khuyenmaiDAO.layDSChuongTrinhKhuyenMai();
        if (dsCTKM != null && dsCTKM.size() > 0) {
            Vector vv = null;
            Chuongtrinhkhuyenmai ctkm = null;
            for (int ix = 0; ix < dsCTKM.size(); ix++) {
                vv = new Vector();
                ctkm = dsCTKM.get(ix);
                vv.add(ctkm.getMakhuyenmai());
                vv.add(ctkm.getNgaybatdau());
                vv.add(ctkm.getNgayketthuc());
                vv.add(ctkm.getHanghoa());
                vv.add(ctkm.getGhichu());
                tableModel.addRow(vv);
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jtDSCTKM = new javax.swing.JTable();
        jbtnCapNhat = new javax.swing.JButton();
        jbtnXoa = new javax.swing.JButton();
        jbtnThem = new javax.swing.JButton();
        jbtnThoat = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("QUAN LY CAC CHUONG TRINH KHUYEN MAI");
        setPreferredSize(new java.awt.Dimension(500, 300));

        jtDSCTKM.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jtDSCTKM.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtDSCTKMMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jtDSCTKM);

        jbtnCapNhat.setText("CAP NHAT");
        jbtnCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCapNhatActionPerformed(evt);
            }
        });

        jbtnXoa.setText("XOA");
        jbtnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnXoaActionPerformed(evt);
            }
        });

        jbtnThem.setText("THEM");
        jbtnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnThemActionPerformed(evt);
            }
        });

        jbtnThoat.setText("THOAT");
        jbtnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(120, 120, 120)
                        .addComponent(jbtnThem, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jbtnXoa, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jbtnCapNhat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbtnThoat)
                        .addGap(18, 18, 18))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 433, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(19, 19, 19))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jbtnCapNhat)
                    .addComponent(jbtnXoa)
                    .addComponent(jbtnThem)
                    .addComponent(jbtnThoat))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnThemActionPerformed
        // TODO add your handling code here:
        ChuongTrinhKhuyenMaiJFrame ctkm = new ChuongTrinhKhuyenMaiJFrame("THEM");
        ctkm.setVisible(true);
    }//GEN-LAST:event_jbtnThemActionPerformed

    private void jbtnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jbtnThoatActionPerformed

    private void jbtnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnXoaActionPerformed
        // TODO add your handling code here:
        //Get ma ctkm. and delete
        if (listSelected == null || listSelected.size() == 0) {
            JOptionPane.showMessageDialog(this, "Hay chon it nhat mot");
            return;
        }
        try {
            for (int ix = 0; ix < listSelected.size(); ix++) {
                khuyenmaiDAO.xoaChuongTrinhKhuyenMai(listSelected.get(ix));
            }
            JOptionPane.showMessageDialog(this, "Xoa thanh cong");
            dsCTKM = khuyenmaiDAO.layDSChuongTrinhKhuyenMai();
            if (dsCTKM != null && dsCTKM.size() > 0) {
                Vector vv = null;
                Chuongtrinhkhuyenmai ctkm = null;
                for (int ix = 0; ix < dsCTKM.size(); ix++) {
                    vv = new Vector();
                    ctkm = dsCTKM.get(ix);
                    vv.add(ctkm.getMakhuyenmai());
                    vv.add(ctkm.getNgaybatdau());
                    vv.add(ctkm.getNgayketthuc());
                    vv.add(ctkm.getHanghoa());
                    vv.add(ctkm.getGhichu());
                    tableModel.addRow(vv);
                }
            }
            jtDSCTKM.setModel(tableModel);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Xoa Khong thanh cong");
        }
    }//GEN-LAST:event_jbtnXoaActionPerformed

    private void jbtnCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnCapNhatActionPerformed
        // TODO add your handling code here:
        if(listSelected == null || listSelected.size()!=1){
            JOptionPane.showMessageDialog(this, "Hay Chon Mot");
            return;
        }
        
        ChuongTrinhKhuyenMaiJFrame ctkm = new ChuongTrinhKhuyenMaiJFrame(listSelected.get(0),"CAP NHAT");
        ctkm.setVisible(true);
    }//GEN-LAST:event_jbtnCapNhatActionPerformed

    private void jtDSCTKMMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtDSCTKMMouseClicked
        // TODO add your handling code here:
        listSelected = new ArrayList<Chuongtrinhkhuyenmai>();
        int[] arrSelected = jtDSCTKM.getSelectedRows();
        int length = arrSelected.length;
        int maCCKM;
        for (int ix = 0; ix < length; ix++) {
            Object obj = jtDSCTKM.getValueAt(ix, 0);
            if(obj instanceof Integer){
                maCCKM = (Integer)obj;
            }else {
                maCCKM = 0;
            }
            listSelected.add(khuyenmaiDAO.layThongTinChuongTrinhKhuyenMai(maCCKM));
        }
    }//GEN-LAST:event_jtDSCTKMMouseClicked
//    /**
//     * @param args the command line arguments
//     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(QuanLyCacChuongTrinhKhuyenMai.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(QuanLyCacChuongTrinhKhuyenMai.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(QuanLyCacChuongTrinhKhuyenMai.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(QuanLyCacChuongTrinhKhuyenMai.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new QuanLyCacChuongTrinhKhuyenMai().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jbtnCapNhat;
    private javax.swing.JButton jbtnThem;
    private javax.swing.JButton jbtnThoat;
    private javax.swing.JButton jbtnXoa;
    private javax.swing.JTable jtDSCTKM;
    // End of variables declaration//GEN-END:variables
}
